package Year22_Month04.day05;

public class 有效的完全平方数 {
    public static boolean isPerfectSquare(int num) {
        int left = 1;
        int right = num;
        while(left <= right){
            int mid = left + ((right - left) >> 1);
            int tmp = num / mid;
            if(tmp > mid){
                left = mid + 1;
            }else if(tmp < mid){
                right = mid - 1;
            }else{
                if(num % tmp == 0) {
                    return true;
                }
                right = mid + 1;
            }
        }
        return false;
    }
    public static boolean isPerfectSquare2(int num) {
        int low = 1;
        int high = num;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            // int product = mid * mid;   越界
            int t = num / mid;
            if (t == mid) {
                if (num%mid == 0) return true;
                low = mid + 1;
            } else if (t < mid) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(isPerfectSquare(2147483647));
    }
}
